<template>
    <div class="head1">
        <img src="@/assets/logo.png" style="margin-left: -80%; margin-top: -9px; width: 15%; height: 200%;" alt=""/>
        <div style="margin-top: -30px">
            <el-link style="margin-left: 80%; width: 7%;  color: #cecece; padding-bottom: 8px" @click="toLogin()">
                <div>{{linkName}}</div>
            </el-link>
            <el-link style="width: 7%;  color: #cecece; padding-bottom: 8px" @click="openForm">退出登录</el-link>
            <el-icon style="color: #cecece;"><Right /></el-icon>
            <el-dialog title="是否要退出登录？" v-model="formVisible">
                <el-text>未登录用户无法使用AI问答、评论等功能</el-text>
                <template v-slot:footer>
                    <div class="dialog-footer">
                        <el-button @click="closeForm">取消</el-button>
                        <el-button type="primary" @click="logout">确定</el-button>
                    </div>
                </template>
            </el-dialog>
        </div>
    </div>

</template>

<style scoped>
.head1{
  background-color: #3d3d3d;
  height: 30%;
  width: 100%;
  padding-top: 15px;
  padding-bottom: 15px;
}
</style>

<script setup>
import {Right} from "@element-plus/icons-vue";
import router from "@/router";
import {ElMessage} from "element-plus";
import {onMounted, ref} from "vue";
let numberValue = -1
let flag = 1
let linkName = ref("")
let formVisible = ref(false)
function openForm(){
    formVisible.value = true
}

function closeForm(){
    formVisible.value = false
}

onMounted( ()=> {
    let url = window.location.href;
    linkName.value = '登录/注册'
    if(url.length > 26){
        url = url.substring(26)
        let index = url.indexOf('/')
        if(index !== -1)    url = url.substring(0, index)
        numberValue = parseInt(url);
        if(numberValue !== -1){
            flag = 0
            linkName.value = '用户主页'
        }
        console.log(numberValue); // 输出提取的数字
    }
});

const toLogin = () =>{
    if(flag === 1)   router.replace('/login');
    else{
        router.replace('/log/'+numberValue.toString()+'/user');
    }
}
function logout(){
    let href = window.location.href;
    let flag = 1
    let numberValue = -1
    let index = -1
    if(href.length > 26){
        let url = href.substring(26)
        index = url.indexOf('/')
        if(index !== -1)    url = url.substring(0, index)
        numberValue = parseInt(url);
        if(numberValue !== -1) flag = 0
        console.log(numberValue); // 输出提取的数字
    }
    if(flag === 1){
        ElMessage.warning('当前尚未登录')
    }
    else{
        ElMessage.success("您已成功退出登录")
        router.replace('/log/-1');
    }
    formVisible.value = false
}
</script>
